﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CuaHangLaptop.BIZ;

namespace QuanLy.GUI
{
    public partial class PhieuNhap_GUI : Form
    {
        List<PhieuNhap_BIZ> list_PN = new List<PhieuNhap_BIZ>();
        List<NhaSX_BIZ> list_NSX = new List<NhaSX_BIZ>();
        PhieuNhap_BIZ current_pn = new PhieuNhap_BIZ();
        bool updated = true;

        public PhieuNhap_GUI()
        {
            InitializeComponent();
            GridView_PN.AutoGenerateColumns = false;
            Frm_Load();
        }

        void Frm_Load()
        {
            list_PN = current_pn.GetAll_PhieuNhap();
            GridView_PN.DataSource = list_PN;
            list_NSX = current_pn.Nhasx.GetAll_NSX();
            cb_NSX.DataSource = list_NSX;
            cb_NSX.DisplayMember = "TenNSX";
            cb_NSX.ValueMember = "MaNSX";
            Disable_All(true);
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            Reset();
            Disable_All(false);
            updated = false;
        }

        private void btn_ChiTiet_Click(object sender, EventArgs e)
        {
            CTPhieuNhap_GUI frm = new CTPhieuNhap_GUI(current_pn.MaPhieuNhap, current_pn.Nhasx.TenNSX);
            frm.Show();
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            try
            {
                if (!updated)
                {
                    bool tt;
                    if (rd_yes.Checked) tt = true;
                    else tt = false;
                    current_pn = new PhieuNhap_BIZ(-1, txt_MaPhieu.Text, cb_NSX.SelectedValue.ToString(), dt_ngay.Value, decimal.Parse(txt_total.Text), tt);
                    current_pn.TotalPrice();
                    list_PN.Add(current_pn);
                }
                else
                {
                    Get_Val();
                    current_pn.TotalPrice();
                }
                
                current_pn.Save();
                GridView_PN.DataSource = null;
                GridView_PN.DataSource = list_PN;
                Disable_All(true);
                updated = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi! " + ex.Message);
            }
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void GridView_PN_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (GridView_PN.Columns[e.ColumnIndex].HeaderText == "Xóa")
            {
                current_pn = (PhieuNhap_BIZ)GridView_PN.Rows[e.RowIndex].DataBoundItem;
                list_PN.Remove(current_pn);
                current_pn.Delete();
                GridView_PN.DataSource = null;
                GridView_PN.DataSource = list_PN;
            }
            if (GridView_PN.Columns[e.ColumnIndex].HeaderText == "Sửa")
            {
                current_pn = (PhieuNhap_BIZ)GridView_PN.Rows[e.RowIndex].DataBoundItem;
                Disable_All(false);
            }
        }

        private void GridView_PN_SelectionChanged(object sender, EventArgs e)
        {
            if (GridView_PN.SelectedRows.Count > 0)
            {
                current_pn = (PhieuNhap_BIZ)GridView_PN.SelectedRows[0].DataBoundItem;
                Set_Val();
                Disable_All(true);
            }
        }

        void Reset()
        {
            int id = current_pn.Get_Lasted_ID();
            txt_MaPhieu.Text = "PN"+(id+1);
            txt_total.Text = "0";
            cb_NSX.SelectedIndex = 0;
            rd_no.Checked = true;
            dt_ngay.Value = DateTime.Now;
        }

        void Set_Val()
        {
            txt_MaPhieu.Text = current_pn.MaPhieuNhap;
            cb_NSX.SelectedValue = current_pn.Nhasx.MaNSX;
            txt_total.Text = current_pn.Tongtien.ToString();
            dt_ngay.Value = current_pn.Ngaylap;
            if (current_pn.Tinhtrang) rd_yes.Checked = true;
            else rd_no.Checked = false;
            dt_ngay.Value = current_pn.Ngaylap;
        }

        void Get_Val()
        {
            current_pn.MaPhieuNhap = txt_MaPhieu.Text;
            current_pn.Nhasx = current_pn.Nhasx.Get_NSX_ByCode(cb_NSX.SelectedValue.ToString());
            current_pn.Tongtien = decimal.Parse(txt_total.Text);
            if (rd_yes.Checked) current_pn.Tinhtrang = true;
            else current_pn.Tinhtrang = false;
            current_pn.Ngaylap = dt_ngay.Value;
        }

        void Disable_All(bool _disable)
        {
            txt_MaPhieu.ReadOnly = _disable;
            cb_NSX.Enabled = rd_no.Enabled = rd_yes.Enabled = dt_ngay.Enabled = !_disable;
        }        
    }
}
